<?php
namespace StarCloud\Models;

use Phalcon\Validation\Validator\StringLength;
use Phalcon\Validation\Validator\InclusionIn;
use Phalcon\Validation\Validator\Between;
use Phalcon\Validation;

trait TraitOrder
{
    public function getOrderSourceType()
    {
        return $this->tableName;
    }

    public function validation()
    {
        $validator = new Validation();

        $validator->add('amount', new Between([
            'minimum' => self::MIN_AMOUNT,
            'maximum' => self::MAX_AMOUNT,
        ]));

        $validator->add(
            ['order_no', 'out_trade_no'],
            new StringLength([
                'min' => ['order_no' => 0, 'out_trade_no' => 0],
                'max' => ['order_no' => self::MAX_LENGTH_ORDER_NO, 'out_trade_no' => self::MAX_LENGTH_OUT_TRADE_NO],
            ])
        );

        $validator->add(
            ['status'],
            new InclusionIn([
                'domain' => [
                    'status' => [
                        self::STATUS_NOT_MAKE_SURE,
                        self::STATUS_CANCEL,
                        self::STATUS_DELIVERY,
                        self::STATUS_MAKE_SURE,
                        self::STATUS_FINISH],
                ],
            ])
        );
        return $this->validate($validator);
    }

    public function getOrderData()
    {
        return json_decode($this->order_data, true);
    }

    // 表的公共字段
    protected $id;
    protected $uid;
    protected $amount; // 使用整数分
    protected $payment; // 支付payment
    protected $order_no;
    protected $out_trade_no;
    protected $status;
    protected $created_at;
    protected $updated_at;
    /**
     * @var array
     */
    protected $order_data;
}
